#ifndef DUYA_CURTAIN_CTRL_DRIVER_H
#define DUYA_CURTAIN_CTRL_DRIVER_H

#include "common.h"
#include "stdint.h"

/**
*说明：杜亚窗帘电机控制协议
*协议格式： 起始码 设备地址 功能码 数据 crc
*			55       12 34 03     01   ad 8a 

*
*
*
*
*
**/

/*调试开关*/
#define DEBUG_DUYE_CURTAIN_CONTROL_ENABLE 0x01

/*最小接收帧长度*/
#define CURTAIN_CONTROL_RECV_FRAME_MINI_LENGTH 0X07
#define CURTAIN_CONTROL_START_CODE 0X55

/**
*数据地址定义 ：
*/
typedef enum 
{
	CURTAIN_IDL = 0X00,				/*ID L*/
	CURTAIN_IDH = 0X01,            /*ID H*/

	CURTAIN_CURRENT_POSITION = 0X02,		/*窗帘当前位置 : 0x00 - 0x64  */
	CURTAIN_MOTOR_DEFAULT_DIRECTION = 0X03,/*电机默认移动方向: 00 默认方向 01 反方向*/
	CURTAIN_PULL_START_ENABLE = 0X04,		/*手拉使能*/
	CURTAIN_MOTOR_STATE = 0X05,				/*电机状态: 00 停  01 开 02 关 03 设置*/

	CURTAIN_PASSIVE_EXTERNAL_SWITCH_TYPE = 0X27, /*干接点开关*/
	CURTAIN_POWERFUL_EXTERNAL_SWITCH_TYPE = 0X28,/*强电开关*/
	CURTAIN_MOTOR_INFO = 0XE0,						/*信息*/
	CURTAIN_DEVICE_TYPE = 0XF0, 					/*设备类型 : 01 开合窗*/
	CURTAIN_MODULE_CHANNLE_NUM = 0XF1,				/*模块通道数 ： 1-15*/
	CURTAIN_SOFT_VERSION = 0XFD,					/*软件版本:0-255*/
	CURTAIN_PROTOCOL_VERSION = 0XFE,				/*协议版本:0xA4*/
	
}STRU_CURTAIN_DATA_ADDRESS_DEFINE;

/**
*控制指令定义
**/
typedef enum 
{
	CURTAIN_OPEN = 0X01,/*窗帘开*/
	CURTAIN_CLOSE = 0X02,/*窗帘关*/
	CURTAIN_STOP = 0X03,	/*窗帘停*/
	CURTAIN_PERCENT = 0X04,/*窗帘进程百分比*/
	CURTAIN_DEL_STROKE = 0X07,/*删除进程*/
	CURTAIN_RELOAD_SETTING = 0X08,/*恢复出厂设置*/
	CURTAIN_SET_SCENE_MODE = 0X09,/*设置情景模式*/
	CURTAIN_RUN_SCENE_MODE = 0X0A,/*运行情景模式*/
	CURTAIN_DEL_SCENE_MODE = 0X0B,/*删除情景模式*/
	CURTAIN_NEGATE = 0X0F,/*取反*/
	
}STRU_CURTAIN_CTRL_COMMAND;

/**
**控制码定义
**/
typedef enum
{
	CURTAIN_READ_FUNCODE = 0X01,    /*控制指令*/
	CURTAIN_WRITE_FUNCODE = 0X02,	/*读指令*/
	CURTAIN_CONTROL_FUNCODE = 0X03,	/*写指令*/
	CURTAIN_SLAVE_REQUEST_FUNCODE = 0X04,/*从机请求指令*/
	
}STRU_CURTAIN_CTRL_FUNCODE;


/**窗帘控制命令信息*/
typedef struct 
{
	uint8_t startcode ;
	uint8_t deviceid[2];
	uint8_t funcode;
	uint8_t datalen;
	uint8_t data[32];
	uint8_t crc[2];
	
}STRUCT_CURTAIN_CONTROL_CMDLINE;

typedef struct
{
	uint32_t serialbuatrate; /*串口波特率*/

	uint8_t serialinitok;		/*串口初始化成功标记: 0 :不成功 1：成功*/
	uint8_t deviceprototype;/*设备支持的协议类型*/
	uint8_t serialchannelid;/*发送数据使用的串口通道id */	
	uint16_t devid;			/*设备id*/
	uint8_t curtaininfo[256];
	
}STRUCT_DUYA_CURTAIN_INFO;

/*存储状态的参数及状态信息*/
extern STRUCT_DUYA_CURTAIN_INFO StruDuyaCurtainInfo;
extern STRUCT_CURTAIN_CONTROL_CMDLINE struCurtainControl;

/*外部接口定义*/
extern void duya_curtain_control_init(uint8_t channel ,uint32_t buatrate,uint8_t initstate);
extern uint8_t duya_curtain_control_pack_data(uint16_t deviceid,uint8_t funcode,uint8_t dataadd,uint8_t srcdatalen,int32_t* srcdata,uint8_t * destdata);
extern uint8_t duya_curtain_control_unpack_data(uint8_t *pdata,uint8_t len,uint16_t devid,uint8_t func,uint8_t dataadd,uint8_t num);
extern uint8_t duya_curtain_control_read_data(uint16_t deviceid,uint8_t funcode,uint8_t dataadd,uint8_t srcdatalen,uint8_t * destdata);
extern uint8_t duya_curtain_get_cmd_channel(uint8_t * channel);

extern uint16_t Get_CRC16(uint8_t *pBuff, uint16_t len);

#endif
